const express = require("express");
const {graphqlHTTP} = require("express-graphql");
const {buildSchema} = require("graphql");

var i = 0;

const schema = buildSchema(`
    type Account {
        name: String
        age: Int
        salary(city: String):Int
    }

    type Query{
       getClassMates(classNo: Int!):[String]
       getAccount(username: String): Account 
    }   
`);

const root = {
    getClassMates({classNo}) {
        const obj = {
            31: ["1", "2", "3"],
            65: ["4", "5", "6", "7"]
        }

        return obj[classNo]
    },
    getAccount({username}) {
        const name = username
        const age = 10
        const salary = ({city}) => {
            if (city === "北京" || city === "上海" || city === "深圳"|| city == "昆明") {

                return 10000
            }
            return 6000
        }
        return {
            name,
            age,
            salary,
        }
    }
};

const app = express();

app.use("/graphql", graphqlHTTP({
    schema: schema,
    rootValue: root,
    graphiql: true,
}));

console.log("启动服务器")
app.listen(4000);

